<?php
    include_once "inc_function.php";     
    ini_set('error_reporting', e_notice);
    include_once '../../function/class_form.php';

	$form = new custom_form();
	 
   global $con;
   $curs_company = oci_new_cursor($con);
   $stmt = oci_parse($con,"begin pkg_master.sp_get_company_list(1,:data);end;");
   oci_bind_by_name($stmt,":data",&$curs_company,-1,OCI_B_CURSOR);
   oci_execute($stmt,OCI_DEFAULT);
   oci_execute($curs_company,OCI_DEFAULT);
   oci_free_statement($stmt);  
  
   $ck_status = ($_REQUEST['chk_log_active'] == 'on' && isset($_REQUEST['chk_log_active'])) ? 1 : 0;
   	switch($_POST['action']){
		case 'add':
			$stmt = oci_parse($con,"begin pkg_master.sp_insert_t_user_login(:s_employee_id,:s_title_id,:s_login_name,:s_login_pass,:s_email,:s_status,:s_person,:s_main_company,:s_login_code);end;");
			oci_bind_by_name($stmt,":s_employee_id",$_POST['emp_id']);	   
			oci_bind_by_name($stmt,":s_title_id",$_POST['job_title_id']);			
			oci_bind_by_name($stmt,":s_login_name",$_POST['txt_login_name']);	
			oci_bind_by_name($stmt,":s_login_pass",encrypt_decrypt('encrypt', $_POST['txt_password'],$con));			
			oci_bind_by_name($stmt,":s_email",$_POST['eml_email']);			
			oci_bind_by_name($stmt,":s_status",$ck_status);			
			oci_bind_by_name($stmt,":s_person",$_POST['person']);			
			oci_bind_by_name($stmt,":s_main_company",$_POST['cmb_company']);			
			oci_bind_by_name($stmt,":s_login_code",$_POST['txt_login_code']);			
			oci_execute($stmt);
			oci_free_statement($stmt);         	
			
			$r = oci_commit($con);
			// Commit the changes to both tables
			if (!$r) 
			{	
				echo "<script>alert(connection failure, please try again!!!)</script>";				
		    	$e = oci_error($con);
				oci_rollback($con);
			}								
			
			echo "<script>window.location.href = '".AJAX_PATH."list_login.php'</script>";
			//header("location : list_employee.php"); --- g bisa redirect php langsung
			break;
		case 'update':	
			$stmt = oci_parse($con,"begin pkg_master.sp_update_t_user_login(:i_login_id,:s_employee_id,:s_title_id,:s_login_name,:s_login_pass,:s_email,:s_status,:s_person,:s_main_company,:s_login_code);end;");			
			oci_bind_by_name($stmt,":i_login_id",$_REQUEST['update']);	   
			oci_bind_by_name($stmt,":s_employee_id",$_POST['emp_id']);	   
			oci_bind_by_name($stmt,":s_title_id",$_POST['job_title_id']);			
			oci_bind_by_name($stmt,":s_login_name",$_POST['txt_login_name']);	
			oci_bind_by_name($stmt,":s_login_pass",encrypt_decrypt('encrypt', $_POST['txt_password'],$con));			
			oci_bind_by_name($stmt,":s_email",$_POST['eml_email']);			
			oci_bind_by_name($stmt,":s_status",$ck_status);			
			oci_bind_by_name($stmt,":s_person",$_POST['person']);			
			oci_bind_by_name($stmt,":s_main_company",$_POST['cmb_company']);			
			oci_bind_by_name($stmt,":s_login_code",$_POST['txt_login_code']);			
			oci_execute($stmt);			
			oci_free_statement($stmt);
         	
			$r = oci_commit($con);
			// Commit the changes to both tables
			if (!$r) 
			{	
				echo "<script>alert(connection failure, please try again!!!)</script>";				
		    	$e = oci_error($con);
				oci_rollback($con);
			}								
			
			echo "<script>window.location.href = '".AJAX_PATH."list_login.php'</script>";
			//header("Location : list_login.php");
			break;
		default:
		break;
	}
	
   	function encrypt_decrypt($condition,$pass,$con){
   		$new_pass = '';
   		switch($condition){
   			case 'encrypt' :
   				$stmt = oci_parse($con,"BEGIN :new_pass := pkg_security_auth.F_ENCRYPT_TEXT(:pass); END;");
   				oci_bind_by_name($stmt,':new_pass',$new_pass,100);
   				oci_bind_by_name($stmt,':pass',$pass);
   				oci_execute($stmt);
   				return "$new_pass";   				
   			break;
   			case 'decrypt' :
   				$stmt = oci_parse($con,"BEGIN :new_pass := pkg_security_auth.F_DECRYPT_TEXT(:pass); END;");
   				oci_bind_by_name($stmt,':new_pass',$new_pass,100);
   				oci_bind_by_name($stmt,':pass',$pass);
   				oci_execute($stmt);
   				return "$new_pass";   				
   			break;
   			default:
   			break;
   		}
   		oci_free_statement($stmt);   		
   	}
   
	function chekedValidation($content,$validation){
      if($content == $validation){
         return 'selected=selected';
      }
   	}
   	
   	if($_REQUEST['form'] == 'update' && isset($_REQUEST['form'])){

			$curs = oci_new_cursor($con);
		   	$stmt = OCIParse($con,"begin pkg_master.sp_get_login_by_id(:employee_id,:c_employee_list); end;");
		   	OCIBindByName($stmt,":employee_id",$_REQUEST['update']);
		   	OCIBindByName($stmt,":c_employee_list",&$curs,-1,OCI_B_CURSOR);
		   	ociexecute($stmt,OCI_DEFAULT);
		   	ociexecute($curs,OCI_DEFAULT);

   	while (OCIFetchInto($curs,&$o_employee_list))
  	   {
  	   	$the_id = $o_employee_list[0];
       	$emp_id   = $o_employee_list[1];
       	$emp_name = $o_employee_list[15];
       	$job_id = $o_employee_list[2];
       	$company = $o_employee_list[12];
         $login_code = $o_employee_list[13];
         $login_name = $o_employee_list[3];      
         $password = encrypt_decrypt('decrypt', $o_employee_list[4],$con);//$o_employee_list[4];
         $email = $o_employee_list[5];         
         $status = $o_employee_list[6];
  	   }
  	   oci_free_statement($stmt);
  	   oci_free_statement($curs);
  	   
  	   $checked = ($status) ? 'checked=checked' : '';
   	}
   	
   	$action = ($_REQUEST['form'] == 'update' && isset($_REQUEST['form'])) ? 'update': 'add';
   	

?>
<body>
<script type="text/javascript">
		function lookup_data(id,nama,job){
			$("#emp_id").val(id);
			$("#job_title_id").val(job);
			$("#txt_emp_name").val(nama);
		}
	</script>
<script type="text/javascript" src="<?= 'http://'.JS_PATH;?>form/js_form.js"></script>

<?php if(isset($_REQUEST['tester']) && $_REQUEST['tester'] == 'true'){ ?>
	<div><?php
		$attribute = array(
			'label' => 'Label Tester',
			'name' => 'tester',
			'id'   => 'tester',
			'class' => 'textbox',
			'value' => 'heeee',
			'action' =>  array(
				'key' => "onblur",
				'val' => "return newCekValidation(txt,this.id)"
			)		
		);
		
		$attribute_cek = array(
			'label' => 'Label Combo',
			'name' => 'tester',
			'id'   => 'tester',
			'class' => 'ck_box',
			'value' => $curs_company,
			'action' =>  array(
				'key' => "onChange",
				'val' => "return newCekValidation(cmb,this.id)"
			)		
		);

		echo $form->input('text',$attribute);
		echo "<br/>";
		echo $form->input('combo',$attribute_cek);
			
	?></div>
	<?php } ?>
	
<form name='frm_login_register' action method='post' id='frm_login_register' onsubmit='return submitValidation(this.id)'>
<input type='hidden' name='action' value='<?= $action ?>' />
<input type='hidden' name='person' value='<?= $_SESSION['employee_id'] ?>' />
<input type='hidden' name='emp_id' id='emp_id' value='<?=  $emp_id;?>' />
<input type='hidden' name='job_title_id' id='job_title_id' value='<?=  $job_id;?>' />

<fieldset class="frm_login">
	<legend><?= ucwords($action) ?> Login Employee</legend>
	
<div class='formInput'>
	<div>
		<label> Login Code <span class='red'>*</span></label> : <input type='text' name='txt_login_code' class='textbox' id='txt_login_code' value='<?= $login_code ?>' onBlur="return newCekValidation('txt',this.id);"/>
		<sup class='red error_txt_login_code hidden'> * Required </sup>
		<input type='checkbox' name='chk_log_active' id='chk_log_active' style='width:30px;' <?= $checked;?>/>Active
	</div>

	<div>
		<label> Login Name <span class='red'>*</span></label> : <input type='text' name='txt_login_name' class='textbox' id='txt_login_name' value='<?= $login_name ?>' onBlur="return newCekValidation('txt',this.id);"/>
		<sup class='red error_txt_login_name hidden'> * Required </sup>
	</div>

   <div>
		<label> Employee Name <span class='red'>*</span></label> : <input type='text' name='txt_emp_name' class='textbox' id='txt_emp_name' value='<?= $emp_name ?>' onBlur="return newCekValidation('txt',this.id);"/>
		<a href='<?= AJAX_PATH?>list_employee.php?auto=true&keepThis=true&TB_iframe=true&height=400&width=500' class='thickbox'><img src='http://<?= IMAGE_PATH?>search.png' style='cursor:pointer;'/></a>
		<sup class='red error_txt_emp_name hidden'> * Required </sup>
   </div>

	<div>
		<label> Password <span class='red'>*</span></label> : <input type='password' name='txt_password' class='textbox' id='txt_password' value='<?= $password ?>' onBlur="return newCekValidation('txt',this.id);"/>
		<sup class='red error_txt_password hidden'> * Required </sup>
	</div>

   <div>
   	<label> Company <span class='red'>*</span></label> :
   	  <select class="ck_box" id="cmb_company" name="cmb_company" onChange="return newCekValidation('cmb',this.id);">
            <option value='0'> --- PILIH --- </option>
            <?php while (OCIFetchInto($curs_company,&$r_company_list)) { ?>
            <option value="<?= $r_company_list[0]?>" <?= chekedValidation($r_company_list[0],$company);?>> <?= $r_company_list[2]?> </option>
            <?php } ?>
   	  </select>
   	  <sup class='red error_cmb_company hidden'> * Required </sup>
   </div>

   <div>
   	<label> Email <span class='red'>*</span></label> : <input type='text' name='eml_email' class='textbox' id='eml_email' value='<?= $email ?>' onBlur="return newCekValidation('eml',this.id);"/>
   	<sup class='red error_eml_email hidden'> * Required </sup>
   </div>

</div>
<div class="mybutton"> <button type="submit" class="button"> Save </button> <button type="reset" class="button"> Reset </button></div>	
</fieldset>
</form>
</body>